package com.ruoyi.ctcx.service;




import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileTypeUtils;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ctcx.bo.FileUploadResBo;

import com.ruoyi.ctcx.config.Global;
import com.ruoyi.ctcx.utils.FileUtils;
import gui.ava.html.parser.HtmlParser;
import gui.ava.html.parser.HtmlParserImpl;
import gui.ava.html.renderer.ImageRenderer;
import gui.ava.html.renderer.ImageRendererImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;


/**
 * @author lss
 * @Description
 * @date 2019/6/16
 */
@Service
public class FileUploadService {


    /**
     * 文件上传
     * @param file
     * @param path
     */
    public FileUploadResBo uploadFile(MultipartFile file, String path) {
        String fileName = file.getOriginalFilename();
        String fileExt = getFileExt(fileName);
        String fileType = FileTypeUtils.getFileType(fileName);
        String filePath = this.getFilePath(fileExt,path);
        String realPath = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL+ initPath(path);
        FileUtils.createDirectory(realPath); // 创建目录
        File f = new File(Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL+filePath);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileUploadResBo resBo = new FileUploadResBo();
        resBo.setFileExt(fileExt);
        resBo.setFileName(fileName);
        resBo.setFileType(fileType);
        resBo.setFilePath("bfiles/"+filePath);
        resBo.setUploadDate(new Date());
        return resBo;
    }


    /**
     * 根据文件名获取文件后缀
     *
     * @param fileName
     * @return
     */
    private String getFileExt(String fileName) {
        String[] strArray = fileName.split("\\.");
        int suffixIndex = strArray.length - 1;
        return strArray[suffixIndex];
    }

    /**
     * 生成文件上传路径
     * @param fileExt
     * @return
     */
    private String getFilePath(String fileExt,String path) {
        String fileNewName = UUID.randomUUID().toString() + StringUtils.DOT + fileExt;
        return initPath(path) + fileNewName;
    }

    /**
     * 生成文件目录结构
     *
     * @param folderPath
     * @return
     */
    private String initPath(String folderPath) {
        if (!folderPath.endsWith(StringUtils.URL_SEP)) {
            folderPath += StringUtils.URL_SEP;
        }
        return folderPath;
    }

    /**
     * 删除文件
     *
     * @param path
     * @return
     */
    public boolean deleteFile(String path) {
        String s = Global.getUserfilesBaseDir() + path;
        boolean flag = FileUtils.deleteFile(Global.getUserfilesBaseDir() + path);
        return flag;
    }

    public String uploadByContent(String content, String path,String fileType) {
        String fileNamePre = IdUtils.simpleUUID();
        //String fileName = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL+ initPath(path) + fileNamePre+"."+fileType;
        //FileUtils.createFile(fileName);
        //FileUtils.writeToFile(fileName, content,"UTF-8",true);


        //System.out.println(Global.USERFILES_BASE_URL+ initPath(path) + fileNamePre+"."+fileType);
        //

        HtmlParser htmlParser = new HtmlParserImpl();
        htmlParser.loadHtml(content);
        // html 是我的html代码
        ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
        imageRenderer.saveImage(Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL+ initPath(path) + fileNamePre+".png");

        return Global.USERFILES_BASE_URL+ initPath(path) + fileNamePre+".png";
    }
}
